package com.demo.dao.impl;

import com.demo.common.StringUtils;
import com.demo.dao.UserDao;
import com.demo.model.auth.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @desc:
 * @author: Ping
 * @date: 2024/9/18 9:52
 */

@Repository
@Slf4j
public class UserDaoImpl implements UserDao {

    @Autowired
    private Connection conn ;

    public Integer insert(User user) {
        /**
         * SQL 逻辑
         */
        String sql = "INSERT INTO tb_user" +
                " (name, password, salt, create_time, gender) " +
                "VALUES" +
                "(?, ?, ?, ? ,?)" ;
        try (
                // try with resource
                PreparedStatement ps
                        = this.conn.prepareStatement(sql) ;
        ) {
            ps.setString(1, user.getName()) ;
            ps.setString(2, StringUtils.md5Encoder(user.getPassword(),user.getSalt())) ;
            ps.setDate(4, new Date(user.getCreateTime().getTime()));
            ps.setString(3, user.getSalt()) ;
            ps.setInt(5, user.getGender()) ;
            return ps.executeUpdate() ;
        } catch (SQLException e) {
            log.error(e.getMessage()) ;
        }
        return 0 ;
    }

    @Override
    public void update(User user) {

    }

    @Override
    public User select(String username, String password) {
        return null;
    }

    @Override
    public Integer delete(int userId) {
        return null;
    }
}
